package global

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/viper"
)


var (
	DB *sql.DB
)

// 初始化mysql连接
func InitMysql() {
	dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=Local",
		viper.GetString("storage.user"),
		viper.GetString("storage.password"),
		viper.GetString("storage.host"),
		viper.GetString("storage.port"),
		viper.GetString("storage.dbname"),
		viper.GetString("storage.charset"),
	)

	DB, _ = sql.Open("mysql", dns)


	// 测试连接
	err := DB.Ping()
	if err != nil {
		panic(err.Error())
	}
}